package com.llkj.qingnianguoxue.circle;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.base.ConstantUserInfo;
import com.llkj.qingnianguoxue.base.MyApplication;
import com.llkj.qingnianguoxue.bean.CircleDetailBean;
import com.llkj.qingnianguoxue.bean.CircleListBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.login.LoginActivity;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.PayMethodViewUtil;
import com.llkj.qingnianguoxue.util.RxBus;
import com.llkj.qingnianguoxue.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.functions.Action1;

public class CircleDetailActivity extends BaseActivity implements PayMethodViewUtil.PaySuccessListener{

    @BindView(R.id.ivCirclePic)
    protected ImageView mIvCirclePic;//圈子图片
    @BindView(R.id.tvCircleName)
    protected TextView mTvCircleName;//圈子名字
    @BindView(R.id.rlOwnerInfo)
    RelativeLayout mOwnerInfo;//圈住头像
    @BindView(R.id.ivOwnerHead)
    protected ImageView mIvOwnerHead;//圈住头像
    @BindView(R.id.tvOwnerName)
    protected TextView mTvOwnerName;//圈住名字
    @BindView(R.id.tvTag)
    protected TextView mTvTag;//圈住标志
    @BindView(R.id.tvDescription)
    protected TextView mTvDescription;//圈子详情
    @BindView(R.id.tvJoinPrice)
    protected TextView mTvJoinPrice;//圈子费用
    @BindView(R.id.tvJoinInterval)
    protected TextView mTvJoinInteral;//圈子时长
    @BindView(R.id.tvApplyJoin)
    protected TextView mTvJoinChat;//申请加入、群聊
    private String mCircleId,mCircleOwnId,mGroupId,mCircleOwnChatId;//圈子id,圈子群主id,群聊id,群主聊天id
    private String mPrice,mCircleDuration;//费用，时长
    private String mCircleName;//圈子名
    private String mIsAdd;//是否为圈子成员
    private Dialog mDialogTips;//提升付费
    private PayMethodViewUtil payMethodViewUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_circle_detail);
        setTitle("圈子", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        ButterKnife.bind(this);

        initData();

        RxBus.getInstance().toObservable(String.class).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                if (s.equals(ConstantUserInfo.REFRESH_CIRCLE)){
                    myFinish();
                }
            }
        });
    }

    private void initData(){
        Intent intent = getIntent();
        mCircleId = intent.getStringExtra("circleId");
        payMethodViewUtil = new PayMethodViewUtil(this,this);

        loadWaitDialog();
        HttpMethod.circleDetail(this,this,mCircleId);
    }

    @OnClick({R.id.tvApplyJoin, R.id.rlOwnerInfo})
    public void onClick(View view){
        boolean isLogin = MyApplication.getInstance().sputil.getBoolean(ConstantUserInfo.ISLOGIN);
        if (!isLogin) {
            dialogConfirm(null, null, null, null).show();
            return;
        }

        switch (view.getId()){
            case R.id.tvApplyJoin://申请加入、群聊
                if("1".equals(mIsAdd)){//群聊
                    MyChatActivity.startGroupChat(this,mGroupId,mCircleId,mCircleName);
                }else{//申请加入
                    if(mDialogTips!=null){
                        mDialogTips.show();
                    }
                }
                break;
            case R.id.rlOwnerInfo://进入群主详情
                Bundle bundle = new Bundle();
                bundle.putString("ownerId",mCircleOwnId);
                startActivity(CircleOwnerDetailActivity.class,bundle);
                break;
        }
    }

    @Override
    protected void dialogRightBtn() {
        super.dialogRightBtn();
        startActivity(LoginActivity.class);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        payMethodViewUtil.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void paySucessResponse(int type) {
//        if(type==0){//三方支付
//
//        }else{//余额
//
//        }

        loadWaitDialog();
        HttpMethod.addGroup(this,this,mGroupId,mPrice,mCircleDuration,mCircleId,String.valueOf(type));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_CIRCLE_DETAIL) {//圈子详情
            final CircleDetailBean detailBean = GsonUtil.GsonToBean(data,CircleDetailBean.class);
            if(detailBean!=null){
                mCircleOwnId = detailBean.getUid();
                mCircleOwnChatId = detailBean.getOwner();
                mGroupId = detailBean.getHx_id();
                mCircleName = detailBean.getName();
                mIsAdd = detailBean.getIs_add();
                tvTitle.setText(mCircleName);
                mTvDescription.setText(detailBean.getDescribe());
                mTvJoinPrice.setText("入圈费用："+detailBean.getPrice()+"银子");
                mTvJoinInteral.setText("入圈时长："+detailBean.getCycle()+"天");
                if("1".equals(mIsAdd)){
                    mTvJoinChat.setText("进入群聊");
                }else{
                    mTvJoinChat.setText("申请加入");
                }
                String uid = MyApplication.getInstance().sputil.getString(ConstantUserInfo.CHAT_LOGIN_ID);
                if(!uid.equals(mCircleOwnChatId)){
                    mTvTag.setVisibility(View.VISIBLE);
                }

                ImageLoadingUtil.loadingCircleBorderImg(mIvCirclePic,detailBean.getImg(), R.color.transparent);
                mTvCircleName.setText(mCircleName);

                if("admin".equals(mCircleOwnChatId)){
                    mOwnerInfo.setVisibility(View.GONE);
                }else{
                    ImageLoadingUtil.loadingCircleBorderImg(mIvOwnerHead,detailBean.getUserpic(), R.color.transparent);
                    mTvOwnerName.setText(detailBean.getUsername());
                }

                mPrice = detailBean.getPrice();
                mCircleDuration = detailBean.getCycle();
                String note = "加入圈子需支付"+mPrice+"元\n费用时限"+mCircleDuration+"天";
                mDialogTips = DialogUtil.dialogConfirm(this, note, null, "考虑一下", "支付", new DialogUtil.DialogClickListener() {
                    @Override
                    public void dialogClick(int type) {
                        if(type==1){
                            payMethodViewUtil.showPayMethodDialog(detailBean.getPrice());
                        }
                    }
                });
            }
        }else if(method == HttpMethod.HTTP_CIRCLE_ADD_GROUP){//6.4加入圈子
            ToastUtil.makeLongText(this,"加入成功");
            mIsAdd = "1";
            mTvJoinChat.setText("进入群聊");
            RxBus.getInstance().post(ConstantUserInfo.REFRESH_CIRCLE);
        }
    }
}
